package com.code.leetcode.idea;

import com.code.leetcode.idea.common.ListNode;

import java.util.HashSet;
import java.util.Set;

public class LeetCodeIdea003 {
    public static void main(String[] args) {
        LeetCodeIdea003 leetCodeIdea003 = new LeetCodeIdea003();
        int a = leetCodeIdea003.lengthOfLongestSubstring("abcabcbb");
        System.out.println(a);

    }
    public int lengthOfLongestSubstring(String s) {
        Set<Character> characterHashSet = new HashSet<Character>();
        int length = s.length();
        int rk = -1, ans = 0;
        for (int i = 0; i < length; i++) {
            if (i != 0) {
                characterHashSet.remove(s.charAt(i - 1));
            }
            while (rk+1 < length &&  !characterHashSet.contains(s.charAt(rk+1))) {
                characterHashSet.add(s.charAt(rk+1));
                rk++;
            }
            ans = Math.max(ans, characterHashSet.size());
        }
        return ans;
    }
}
